package com.mall.huohuo.process.container;
import	java.util.Objects;

import com.mall.huohuo.process.Node;
import com.mall.huohuo.process.annotation.NodeDefinition;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

/**
 * @author zycoding
 * @version NodeComponetBeanProcesstor.java, v 0.1 2020-08-17 22:16 zycoding
 */
@Component
public class NodeContainerBeanPostProcessor extends AbstractNodeContainer implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        final NodeDefinition annotation = AnnotationUtils.findAnnotation(bean.getClass(), NodeDefinition.class);

        if (Objects.isNull(annotation)) {
            return bean;
        }

        if (!(bean instanceof Node)) {
            throw new RuntimeException("添加@Node的类必须要实现Node接口。");
        }

        final String[] names = annotation.names();
        if (Objects.isNull(names) || names.length == 0) {
            throw new RuntimeException("节点必须要归属与某一个流程下面");
        }

        for (String name : names) {
            registerPipline(annotation,(Node)bean, name);
        }

        return bean;
    }


}
